/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.report.entity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class BaocaoPro implements Serializable{
    private String ngay;
    private String dichvu;
    private int SLDangKi;
    private int SLHuy;

    public String getNgay() {
        return ngay;
    }

    public void setNgay(String ngay) {
        this.ngay = ngay;
    }

    public String getDichvu() {
        return dichvu;
    }

    public void setDichvu(String dichvu) {
        this.dichvu = dichvu;
    }

    public int getSLDangKi() {
        return SLDangKi;
    }

    public void setSLDangKi(int SLDangKi) {
        this.SLDangKi = SLDangKi;
    }

    public int getSLHuy() {
        return SLHuy;
    }

    public void setSLHuy(int SLHuy) {
        this.SLHuy = SLHuy;
    }
    
    public static boolean checkAdmin(long userId,Connection cn) throws Exception{        
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "";        
        try {
            ResourceBundle resource = ResourceBundle.getBundle("config");
            String adminGroupName = resource.getString("adminGroupName");
            strSQL = " select a.user_id,b.group_id,c.group_name "
                    + " from sec_user a,sec_group_user b,sec_group c"
                    + " where a.user_id = b.user_id and c.group_id = b.group_id and a.user_id = "+userId+"";            
            if (strSQL != "") {
                st = cn.createStatement();
                rs = st.executeQuery(strSQL);
                while (rs.next()) {          
                    if(rs.getString(3).toString().toLowerCase().equals(adminGroupName.toLowerCase())){
                        return true;
                    }
                }
            }
        }finally {
            Database.closeObject(rs);
            Database.closeObject(st);
        }
        return false;
    }    
    public static ArrayList<Doitac> listDoitac(long userId) throws Exception{
        ArrayList<Doitac> list = new ArrayList<Doitac>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "";
        try {
            cn = AppServer.getConnection();
            if(checkAdmin(userId,cn)){                
                strSQL = "select partners.partner_id,partners.partner_name from partners";
            }            
            else{                
                strSQL = "select a.partner_id,a.partner_name "
                    + " from partners a, sec_user b, sec_group c, sec_group_user d"
                    + " where a.partner_code = c.group_name and b.user_id = d.user_id"
                    + " and c.group_id = d.group_id"
                    + " and b.user_id = "+userId+"";
            }
            if (strSQL != "") {                
                st = cn.createStatement();
                rs = st.executeQuery(strSQL);
                while (rs.next()) {          
                    Doitac dt = new Doitac(rs.getString(1), rs.getString(2));
                    list.add(dt);
                }
            }
        }finally {
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }
    public static ArrayList<Sanpham> listSanpham(long userId) throws Exception{
        ArrayList<Sanpham> list = new ArrayList<Sanpham>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "";
        try {
            cn = AppServer.getConnection();
            if(checkAdmin(userId,cn)){
                strSQL = "select sdp_products.product_id,sdp_products.product_name from sdp_products";
            }
            else strSQL = "select b.product_id,b.product_name from sdp_product_partner a,sdp_products b"
                    + " where a.product_id = b.product_id and partner_id in ("
                    + " select a.partner_id from partners a, sec_user b, sec_group c, sec_group_user d"
                    + " where a.partner_code = c.group_name and b.user_id = d.user_id"
                    + " and c.group_id = d.group_id"
                    + " and b.user_id = "+userId+") ";                    
            if (strSQL != "") {
                st = cn.createStatement();
                rs = st.executeQuery(strSQL);
                while (rs.next()) {           
                    Sanpham dv = new Sanpham(rs.getString(1), rs.getString(2));
                    list.add(dv);
                }
            }
        } finally {
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }            
}
